<?php

class editLogo extends CourseController {

	function index($name, $invalid = false ) {
		$this->org = $this->orgs->getOrgByName( $name );
		$templateVars =  array(
			'title' =>  $this->org[0]['description'] .' - Change Logo - FEELS ',
			'heading' => $this->org[0]['description'],
			'content' => ($invalid ? 'File uploaded is not a valid image.' : '')
				. BufferedView::instance('org/forms/logo', array(
					'action' => "/org/view/$name/edit-logo/submit",
					'hiddenInputs' => array( 'org_id' => $this->org[0]['org_id'] ),
					'info' => $this->org[0]['css'],
					'orgname' => $name,
					'label' => 'Logo'
				) )->getBuffer(),
			'highlight' => 'orginfo',
			'currentOrg' => $name
		);
	}

	function invalid($name) {
		$this->index($name, true);
	}

	function submit($name) {
		try {
			JF_ImageFile::instance(
				JF_ImageData::fromFile( $_FILES['logo']['tmp_name'] )
				->resize(100,100)
			)->saveAsJPG("./photos/$name.jpg");
			header("Location: /org/view/$name");
		}
		catch ( JF_ImageDataException $e ) {
			if( $e->getCode() === JF_ImageDataException::INVALID_FORMAT ) header("Location: /org/view/$name/edit-logo/invalid");
		}
	}
	
}
